package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.activity.CalenderNoteEditActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.Utils;

import java.util.Date;
import java.util.Map;

/**
 * 常用提醒详情对话框
 * Created by liwei on 2017/4/18.
 */

public class YuyinDetailDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    /**
     *
     * @param context
     * @param themeResId
     * @param windowManager
     * @param date
     * @param time
     * @param Content
     * @param displaytime
     * @param postpone
     * @param before
     * @param alamsoundDesc
     * @param alamsound
     */
    public YuyinDetailDialog(@NonNull final Context context, @StyleRes final int themeResId, final WindowManager windowManager,
                             final String date, final String time,final String Content,final int displaytime, final int postpone, final String before,
                             final String alamsoundDesc, final String alamsound) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);
        String date_str = date;

        View view = LayoutInflater.from(context).inflate(R.layout.reminder_detail_dialog,null);
        setContentView(view);

        final TextView content = (TextView) view.findViewById(R.id.content);
        content.setText(Content);

        TextView time_tv = (TextView) view.findViewById(R.id.time);
        if ((date_str + " " + time).equals(DateUtil.formatDateTime(new Date()))) {
            time_tv.setText(date_str.substring(5, date_str.length()) + "  "+ DateUtil.Week(date) + "  待办  现在");
        } else {
            Date formTime = DateUtil.parseDateTime(date_str + " " + time);
            Date dateToday = DateUtil.parseDateTime(DateUtil.formatDateTime(new Date()));
            long betweem = (formTime.getTime() - dateToday.getTime()) / 1000;
            long day = betweem / (24 * 3600);
            long hour = betweem % (24 * 3600) / 3600;
            long min = betweem % 3600 / 60;
            long C;
            String timeType;
            if (day >= 1) {
                C = Math.abs(day);
                timeType = "天后";
            } else if (day < 0 ){
                C = Math.abs(day);
                timeType = "天前";
            } else {
                if (hour >= 1) {
                    C = Math.abs(hour);
                    timeType = "小时后";
                } else if (hour < 0) {
                    C = Math.abs(hour);
                    timeType = "小时前";
                } else {
                    if (min >= 1) {
                        timeType = "分钟后";
                        C = Math.abs(min);
                    } else {
                        timeType = "分钟前";
                        C = Math.abs(min);
                    }
                }
            }
            if (date_str.substring(0, 4).equals(DateUtil.formatDateYYYY(new Date()))) {
                date_str = date_str.substring(5, date_str.length());
            }

//            time_tv.setText(date_str + "  "+ time + "  "+ DateUtil.Week() +"\n"+C+timeType);
            String c = date_str + "  "+ time + "  "+ DateUtil.Week(date) +"\n"+C+timeType;
            String b = C+timeType;
            SpannableString spannableString = new SpannableString(date_str + "  "+ time + "  "+ DateUtil.Week(date) +"\n"+C+timeType);
            spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.gongkai_txt3)),
                    c.length() - b.length(), c.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            spannableString.setSpan(new AbsoluteSizeSpan(Utils.dipTopx(context, 14)),
                    c.length() - b.length(), c.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            time_tv.setText(spannableString);
        }


        view.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getDBcApplication().insertScheduleData(Content, date,
                        time, 1, Integer.parseInt(before), displaytime, postpone, 0,
                        0, 0, DateUtil.formatDateTime(new Date()), "", 0,
                        "", "", 0, "", DateUtil.formatDateTime(new Date()),
                        1, 0, 0, alamsoundDesc, alamsound, "", 0, 0, 0, "", "", 0, 0, 0,0,"","0","0");
                callBack.abcdefg();
                dismiss();
            }
        });

        view.findViewById(R.id.code).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    int schID = App.getDBcApplication().getScheduleTableMinId() - 1;
                    App.getDBcApplication().insertScheduleData(Content, date,
                            time, 1,
                            Integer.parseInt(before), displaytime, postpone, 0,
                            0, 0, DateUtil.formatDateTime(new Date()), "", 0,
                            "", "", 0, "", DateUtil.formatDateTime(new Date()),
                            1, 0, 0, alamsoundDesc, alamsound, "", 0, 0, 0, "",
                            "", 0, 0, 0,0,"","0","0");
                    callBack.abcdefg();
                    Map<String, String> map = App.getDBcApplication().querayOneCalenderData(schID);
                    Intent cintent = new Intent(context, CalenderNoteEditActivity.class);
                    cintent.putExtra("lingshengnames", map.get(ScheduleTable.schRingDesc));
                    cintent.putExtra("lingshengcode", map.get(ScheduleTable.schRingCode));
                    cintent.putExtra("schTime", map.get(ScheduleTable.schTime));
                    cintent.putExtra("schDate", map.get(ScheduleTable.schDate));
                    cintent.putExtra("schIsAlarm", map.get(ScheduleTable.schIsAlarm));
                    cintent.putExtra("schBeforeTime", map.get(ScheduleTable.schBeforeTime));
                    cintent.putExtra("schDisplayTime", map.get(ScheduleTable.schDisplayTime));
                    cintent.putExtra("schIsPostpone", map.get(ScheduleTable.schIsPostpone));
                    cintent.putExtra("content", map.get(ScheduleTable.schContent));
                    cintent.putExtra("schSourceDesc", map.get(ScheduleTable.schSourceDesc));
                    cintent.putExtra("schImagePath", map.get(ScheduleTable.schImagePath));
                    cintent.putExtra("schID", map.get(ScheduleTable.schID));
                    cintent.putExtra("schIsEnd", map.get(ScheduleTable.schIsEnd));
                    cintent.putExtra("schRepeatID", map.get(ScheduleTable.schRepeatID));
                    context.startActivity(cintent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(context, "操作错误,请重试", Toast.LENGTH_SHORT).show();
                }
                dismiss();
            }
        });

        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }


    public interface CallBack{
        void abcdefg();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


}
